package models

import (
	"context"
	"strings"

	orm "go-papers/database"
	"go-papers/tools"

	"go-papers/pkg/lognew"
)

type Login struct {
	Username  string `form:"UserName" json:"username" binding:"required"`
	Password  string `form:"Password" json:"password" `
	LoginType string `form:"LoginType" json:"loginType" `
	OpenId    string `form:"OpenId" json:"openId" `
	Code      string `form:"Code" json:"code" `
	UUID      string `form:"UUID" json:"uuid" `
	MomCode   string `form:"momCode" json:"momCode" `
}

func (u *Login) GetUser() (user SysUser, role SysRole, e error) {

	e = orm.Eloquent.Table("sys_user").Where("username = ? ", u.Username).Find(&user).Error
	if e != nil {
		lognew.Error(context.TODO(), "账号不存在(代码204)", lognew.Fields{"err": e.Error(), "data": u.Username})
		return
	}
	_, e = tools.CompareHashAndPassword(user.Password, u.Password)
	if e != nil {
		if strings.Contains(e.Error(), "hashedPassword is not the hash of the given password") {
			tools.HasError(e, "账号或密码错误(代码201)", 500)
		}
		lognew.Error(context.TODO(), "密码错误(代码201)", lognew.Fields{"err": e.Error(), "data": u.Username, "pwd": user.Password})
		return
	}
	e = orm.Eloquent.Table("sys_role").Where("role_id = ? ", user.RoleId).First(&role).Error
	if e != nil {
		lognew.Error(context.TODO(), "用户角色错误错误(代码201)", lognew.Fields{"err": e.Error(), "user": user})
		return
	}
	return
}

//BibdMomByCode
func (u *Login) BindMomByCode(mid int, momCode string) (res bool, e error) {

	// 更新用户管家妈妈
	if u.MomCode != "" {
		return
	}

	// 查询 code 的momid
	var mom Member
	e = orm.Eloquent.Table("member").Where("member_code = ? ", momCode).Find(&mom).Error
	if e != nil {
		lognew.Error(context.TODO(), "BindMomByCode_ERROR", lognew.Fields{"u": u.Username, "p": u.Password, "e": e.Error()})
		return
	}

	var user Member
	user.MomUserId = mom.MemberId
	user.MomUserCode = mom.MemberCode
	user.MomRealName = mom.RealName
	_, err := user.Update(mid)
	return true, err
}

func (u *Login) GetMemberUser() (user Member, e error) {

	e = orm.Eloquent.Table("member").Where("account_name = ? ", u.Username).Find(&user).Error
	if e != nil {
		if strings.Contains(e.Error(), "record not found") {
			tools.HasError(e, "账号或密码错误(代码204)", 500)
		}
		lognew.Error(context.TODO(), "GetMemberUser_ERROR", lognew.Fields{"u": u.Username, "p": u.Password, "e": e.Error()})
		return
	}

	if user.Password != "" && u.Password != "" {
		_, e = tools.CompareHashAndPassword(user.Password, u.Password)
		if e != nil {
			if strings.Contains(e.Error(), "hashedPassword is not the hash of the given password") {
				tools.HasError(e, "账号或密码错误(代码201)", 500)
			}
			lognew.Error(context.TODO(), "GetMemberUser_ERROR", lognew.Fields{"u": u.Username, "p": u.Password, "e": e.Error()})
			return
		}
	} else {
		//手机号登陆
		if u.Code != "" {

		} else {
			return
		}
	}

	//当前是否有 openId
	if u.OpenId != "" {
		//查询是否有记录
		var thirdUser ThirdUser
		er := orm.Eloquent.Table("third_user").Where("thrid_open_id = ? ", u.OpenId).Find(&thirdUser).Error
		if er != nil {
			return
		}

		var memberThird MemberThirdInfo
		er = orm.Eloquent.Table("member_third_info").Where("third_user_id = ? AND status = 1", thirdUser.ThridUserId).Find(&memberThird).Error
		if er != nil {
			//新增记录
			memberThird.MemberId = user.MemberId
			memberThird.Status = 1
			_, err := memberThird.Create()
			if err != nil {
				lognew.Error(context.TODO(), "memberThirdCreate", lognew.Fields{"user": user, "err": err.Error()})
			}
			return
		}

		//更新记录
		memberThird.MemberId = user.MemberId
		memberThird.Status = 1
		_, _ = memberThird.Update(memberThird.Id)

	}

	return
}

func (u *Login) GetThirdMemberUser() (user Member, e error) {

	var thirdUser ThirdUser
	e = orm.Eloquent.Table("third_user").Where("third_open_id = ? ", u.OpenId).Find(&thirdUser).Error
	if e != nil {
		if strings.Contains(e.Error(), "record not found") {
			tools.HasError(e, "OpenID 未授权 (代码204)", 500)
		}
		lognew.Error(context.TODO(), "GetThirdMemberUser_ERROR", lognew.Fields{"u": u.Username, "p": u.Password, "e": e.Error()})
		return
	}

	var memberThird MemberThirdInfo
	e = orm.Eloquent.Table("member_third_info").Where("third_user_id = ? AND status = 1", thirdUser.ThridUserId).Find(&memberThird).Error
	if e != nil {
		if strings.Contains(e.Error(), "record not found") {
			tools.HasError(e, "未绑定账户(代码204)", 500)
		}
		lognew.Error(context.TODO(), "GetMemberUser_ERROR", lognew.Fields{"u": u.Username, "p": u.Password, "e": e.Error()})
		return
	}
	//第三方关联账户
	e = orm.Eloquent.Table("member").Where("member_id = ? ", memberThird.MemberId).Find(&user).Error
	return
}
